package com.zide.video.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zide.ams.domain.po.AmsCategory;
import com.zide.ams.mapper.AmsCategoryMapper;
import com.zide.common.core.domain.PageResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.zide.common.core.vo.CommonParamIdVO;
import com.zide.common.utils.DateUtils;
import com.zide.video.domain.po.Video;
import com.zide.video.domain.vo.result.VideoResultVO;
import com.zide.video.domain.vo.save.VideoSaveVO;
import com.zide.video.domain.vo.search.VideoSearchVO;
import com.zide.video.mapper.VideoMapper;
import com.zide.video.service.IVideoService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 视频信息Service业务层处理
 * 
 * @author zide
 * @date 2024-05-08
 */
@Slf4j
@Service
public class VideoServiceImpl implements IVideoService {
    @Resource
    private VideoMapper videoMapper;

    @Resource
    private AmsCategoryMapper amsCategoryMapper;
    /**
     * 查询视频信息列表
     */
    @Override
    public PageResult<VideoResultVO> searchVideoPageList(VideoSearchVO searchVO) {
        LambdaQueryWrapper<Video> wrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotNull(searchVO.getCategoryId())) {
            Long categoryId = searchVO.getCategoryId();
            AmsCategory amsCategory = amsCategoryMapper.selectById(categoryId);
            if(ObjectUtil.isNotNull(amsCategory)) {
                Long parentId = amsCategory.getParentId();
                if(parentId == 0) {
                    List<AmsCategory> amsCategories = amsCategoryMapper.selectList(new LambdaUpdateWrapper<AmsCategory>()
                            .eq(AmsCategory::getParentId, amsCategory.getId()));
                    if(CollectionUtil.isNotEmpty(amsCategories)) {
                        wrapper.in(Video::getCategoryId,amsCategories.stream().map(AmsCategory::getId).collect(Collectors.toList()));
                    }else {
                        wrapper.eq(Video::getCategoryId,searchVO.getCategoryId());
                    }

                }else {
                    wrapper.eq(Video::getCategoryId,searchVO.getCategoryId());
                }
            }
        }
        PageResult<Video> dbPageResult = videoMapper.selectPage(searchVO,wrapper);
        List<VideoResultVO> resultList = BeanUtil.copyToList(dbPageResult.getList(),VideoResultVO.class);
        PageResult<VideoResultVO> result = new PageResult<>();
        result.setTotal(dbPageResult.getTotal());
        result.setList(resultList);
        return result;
    }

    @Override
    public List<VideoResultVO> searchHotList() {
        LambdaQueryWrapper<Video> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Video::getReadVolume);
        wrapper.last("limit 8");
        List<Video> amsArticleList = videoMapper.selectList(wrapper);
        return BeanUtil.copyToList(amsArticleList,VideoResultVO.class);
    }

    /**
     * 查询视频信息详情
     */
    @Override
    public VideoResultVO selectVideoById(Long id) {
            Video dbInfo = videoMapper.selectById(id);
            VideoResultVO result = BeanUtil.copyProperties(dbInfo,VideoResultVO.class);
        return result;
    }

    /**
     * 新增视频信息
     */
    @Override
    public int saveVideo(VideoSaveVO saveVO) {
        Video insertBean = BeanUtil.copyProperties(saveVO,Video.class);
        return videoMapper.insert(insertBean);
    }

    /**
     * 修改视频信息
     */
    @Override
    public int updateVideo(VideoSaveVO saveVO) {
        Video updateBean = BeanUtil.copyProperties(saveVO,Video.class);
        return videoMapper.updateById(updateBean);
    }

    /**
     * 批量删除视频信息
     */
    @Override
    public int removeVideoByIds(CommonParamIdVO idVO) {
        if(ArrayUtil.isNotEmpty(idVO.getIds())) {
            return videoMapper.deleteBatchIds(Arrays.asList(idVO.getIds()));
        }else {
            return videoMapper.deleteById(idVO.getId());
        }

    }
}
